<template>
  <div>
    <div ref="chart" style="width: 100%; height: 400px;">

    </div>
  </div>
</template>

<script>
import * as echarts from 'echarts';
import 'echarts/lib/component/tooltip';
import 'echarts/lib/component/legend';
import { exceptList } from "@/api/vendor/fig";

export default {
  data() {
    return {
      myChart: null,
      list: {},
      option : {
        tooltip: {
          trigger: 'item'
        },
        legend: {
          top: '5%',
          left: 'center'
        },
        series: [
          {
           // name: 'Access From',
            type: 'pie',
            radius: ['40%', '70%'],
            avoidLabelOverlap: false,
            label: {
              show: false,
              position: 'center'
            },
            emphasis: {
              label: {
                show: true,
                fontSize: 40,
                fontWeight: 'bold'
              }
            },
            labelLine: {
              show: false
            },
            data: [
              // { value: 1048, name: 'Search Engine' },
              // { value: 735, name: 'Direct' },
              // { value: 580, name: 'Email' },
              // { value: 484, name: 'Union Ads' },
              // { value: 300, name: 'Video Ads' }
            ]
          }
        ]
      }
    };
  },
  mounted() {
    this.myChart = echarts.init(this.$refs.chart, null, {
      renderer: 'canvas',
      useDirtyRect: false
    });
    this.renderChart();
    window.addEventListener('resize', this.handleResize);
  },
  created() {
    this.getList();
  },
  methods: {
      getList() {
          exceptList().then(response => {
            const le = response.total;
           // console.log("list1:",response);
            this.list = {
                  rows: [...response.rows.map(row => ({
                    demandLineStatus: row.demandLineStatus,
                    dictLabel: row.dictLabel
                  }))],
                };
          // console.log("111:",this.list.rows)
            // 清空原有数据
               this.option.series[0].data = [];

               for (let i = 0; i < le; i++) {
                 this.option.series[0].data.push({
                   value: this.list.rows[i].demandLineStatus  ,
                   name: this.list.rows[i].dictLabel
                 });
               //this.option.series[0].name.push(this.list.rows[0].dictLabel);
               }

             ///  console.log("总分:", this.option.series[0].data);
            this.renderChart(); // 在数据更新后重新渲染图表
          });
        },
    renderChart() {


      this.myChart.setOption(this.option);
    },
    handleResize() {
      this.myChart.resize();
    }
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.handleResize);
  }
};
</script>
